package com.lvmama.pangolin.task;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.lvmama.pangolin.common.Constant;
import com.lvmama.pangolin.conf.ConfigManager;
import com.lvmama.pangolin.entity.ConvertTask;
import com.lvmama.pangolin.proces.MTTicketDataProcess;
import com.lvmama.pangolin.proces.QNRTicketDataProcess;
import com.lvmama.pangolin.proces.TC_Ticket_Data_Process;
import com.lvmama.pangolin.proces.TNTicketDataProcess;
import com.lvmama.pangolin.proces.XCTicketDataProcess;
import com.lvmama.pangolin.service.ConvertTaskService;
@Component
@EnableScheduling
public class DataFormatTask {
	/**  
	* @Title：DataFormatTask.java
	* @Author:zhoutianqi
	* @Description:定期扫描Timmer任务，转化数据
	* @Date：2017年2月13日下午8:07:25  
	*/
	private Logger logger = Logger.getLogger(DataFormatTask.class);
	private static String sys_code = "";
	private static boolean  CONFIGMANAGER_DONE = false;
	private static List<ConvertTask> qnrTaskList = new ArrayList<>();
	private static List<ConvertTask> tcTaskList = new ArrayList<>();
	private static List<ConvertTask> tnTaskList = new ArrayList<>();
	private static List<ConvertTask> xcTaskList = new ArrayList<>();
	private static List<ConvertTask> mtTaskList = new ArrayList<>();
	@Autowired
	private ConfigManager configManager;
	@Autowired
	private ConvertTaskService taskService;
	@Autowired
	private QNRTicketDataProcess qnrTicketDataProcess;
	@Autowired
	private TC_Ticket_Data_Process tcTicketDataProcess;
	@Autowired
	private TNTicketDataProcess tnTicketDataProcess;
	@Autowired
	private XCTicketDataProcess xcTicketDataProcess;
	@Autowired
	private MTTicketDataProcess mtTicketDataProcess;
	@Value("#{'${taskLimitNum}'}")
	private String taskLimitNum;
	@Value("#{'${taskDefaultLimitNum}'}")
	private String taskDefaultLimitNum;
	
	@Scheduled(cron = "${Formate_Task_TimeRate}")
	public void scheduledMothod(){
		Map<String,Object> paramMap = new HashMap<String,Object>();
		paramMap.put("status", Constant.DATA_RUNNING_STATE_CODE);
		paramMap.put("businessType", "TICKET");
		int processNum = taskService.queryByMap(paramMap).size();
		if(processNum>0){return;}//线程中任务处理完，再缓存任务
		qnrTaskList.clear();
		tcTaskList.clear();
		tnTaskList.clear();
		xcTaskList.clear();
		mtTaskList.clear();
		cacheConvertTask();
		formateConvertTask();
	}

    public void cacheConvertTask() {
    	Map<String,Object> paramMap = new HashMap<>();
		int limitNum = 0;
		if(taskLimitNum!=null && !taskLimitNum.equals("")){
			//获取指定条数的Timer
			limitNum=Integer.parseInt(taskLimitNum);
		}else{
			//获取default条数的Timer
			limitNum=Integer.parseInt(taskDefaultLimitNum);
		}
		paramMap.put("limitNum", limitNum);
		paramMap.put("businessType", "TICKET");
		//获取状态是0,24 并且转化失败次数小于最大校验次数的timer
		List<ConvertTask> taskList1 = null;
		try {
			taskList1 = taskService.queryByLimitNum(paramMap);
			if(taskList1==null || taskList1.size()==0){return;}
		} catch (Exception e) {
			logger.error("!!!!!!!!!!!!!DataFormatTask缓存任务异常!");
		}
		Map<String,Object> param1 = new HashMap<String,Object>();
		//更新timer状态：处理中
		for(ConvertTask item:taskList1){
			param1.put("status", Constant.DATA_RUNNING_STATE_CODE);
			param1.put("id", item.getId());
			try {
				taskService.updateByMap(param1);
				if(item.getCompanyCode().equals("QNR_C_CODE")){
					qnrTaskList.add(item);
				}else if(item.getCompanyCode().equals("TC_C_CODE")){
					tcTaskList.add(item);
				}else if(item.getCompanyCode().equals("TN_C_CODE")){
					tnTaskList.add(item);
				}else if(item.getCompanyCode().equals("XC_C_CODE")){
					xcTaskList.add(item);
				}else if(item.getCompanyCode().equals("MT_C_CODE")){
					mtTaskList.add(item);
				}
			} catch (Exception e) {
				logger.info("!!!!!!!!!!!!!DataFormatTask更新任务"+item.getId()+"状态异常!");
			}
		}
		return;
    }
    public void formateConvertTask() {
		if(!CONFIGMANAGER_DONE){configManager.init(sys_code);CONFIGMANAGER_DONE=true;}
		try {
			if(qnrTaskList.size()>0){
				qnrTicketDataProcess.executeProcess(qnrTaskList);
			}
			if(tcTaskList.size()>0){
				tcTicketDataProcess.executeProcess(tcTaskList);
			}
			if(tnTaskList.size()>0){
				tnTicketDataProcess.executeProcess(tnTaskList);
			}
			if(xcTaskList.size()>0){
				xcTicketDataProcess.executeProcess(xcTaskList);
			}
			if(mtTaskList.size()>0){
				mtTicketDataProcess.executeProcess(mtTaskList);
			}
		} catch (Exception e) {
			logger.error("!!!!!!!!!!!!!DataFormatTask任务分发状态异常!");
		}
		
    }
}
